/**
 * 格式化内容
 * @param {string} str 源内容
 * @returns 格式化后内容
 */
 function format(str) {
  if (str.includes('0⃣')) {
    const regex = new RegExp('0⃣', 'g')
    str = str.replace(regex, `0`)
  }
  if (str.includes('1⃣')) {
    const regex = new RegExp('1⃣', 'g')
    str = str.replace(regex, `1`)
  }
  if (str.includes('2⃣')) {
    const regex = new RegExp('2⃣', 'g')
    str = str.replace(regex, `2`)
  }
  if (str.includes('3⃣')) {
    const regex = new RegExp('3⃣', 'g')
    str = str.replace(regex, `3`)
  }
  if (str.includes('4⃣')) {
    const regex = new RegExp('4⃣', 'g')
    str = str.replace(regex, `4`)
  }
  if (str.includes('5⃣')) {
    const regex = new RegExp('5⃣', 'g')
    str = str.replace(regex, `5`)
  }
  if (str.includes('6⃣')) {
    const regex = new RegExp('6⃣', 'g')
    str = str.replace(regex, `6`)
  }
  if (str.includes('7⃣')) {
    const regex = new RegExp('7⃣', 'g')
    str = str.replace(regex, `7`)
  }
  if (str.includes('8⃣')) {
    const regex = new RegExp('8⃣', 'g')
    str = str.replace(regex, `8`)
  }
  if (str.includes('9⃣')) {
    const regex = new RegExp('9⃣', 'g')
    str = str.replace(regex, `9`)
  }
 
  if (str.includes('0️⃣')) {
    const regex = new RegExp('0️⃣', 'g')
    str = str.replace(regex, `0`)
  }
  if (str.includes('1️⃣')) {
    const regex = new RegExp('1️⃣', 'g')
    str = str.replace(regex, `1`)
  }
  if (str.includes('2️⃣')) {
    const regex = new RegExp('2️⃣', 'g')
    str = str.replace(regex, `2`)
  }
  if (str.includes('3️⃣')) {
    const regex = new RegExp('3️⃣', 'g')
    str = str.replace(regex, `3`)
  }
  if (str.includes('4️⃣')) {
    const regex = new RegExp('4️⃣', 'g')
    str = str.replace(regex, `4`)
  }
  if (str.includes('5️⃣')) {
    const regex = new RegExp('5️⃣', 'g')
    str = str.replace(regex, `5`)
  }
  if (str.includes('6️⃣')) {
    const regex = new RegExp('6️⃣', 'g')
    str = str.replace(regex, `6`)
  }
  if (str.includes('7️⃣')) {
    const regex = new RegExp('7️⃣', 'g')
    str = str.replace(regex, `7`)
  }
  if (str.includes('8️⃣')) {
    const regex = new RegExp('8️⃣', 'g')
    str = str.replace(regex, `8`)
  }
  if (str.includes('9️⃣')) {
    const regex = new RegExp('9️⃣', 'g')
    str = str.replace(regex, `9`)
  }
  if (str.includes('🔟')) {
    const regex = new RegExp('🔟', 'g')
    str = str.replace(regex, `10`)
  }
  if (str.includes('🉑')) {
    const regex = new RegExp('🉑', 'g')
    str = str.replace(regex, `可`)
  }
  if (str.includes('🈶')) {
    const regex = new RegExp('🈶', 'g')
    str = str.replace(regex, `有`)
  }
  str = str.replaceAll(/➕/g, '+')
  return str
}
 
function doMatch(data, keywords) {
  const result = keywords.map(e => {
    let res = null;
    if (!e.isPrivate) {
      try {
        const matched = data.match(e.keyword)
        if (matched) {
          res = { keyword: e, matched }
        }
      } catch (err) {
        // console.error(err)
      }
    }
    if (res) {
      return res
    } else if (data.includes(e.keyword)) {
      return { keyword: e, matched: [ e.keyword ] }
    }
    return null
  })
  return result.filter(e => e).sort((o1, o2) => o2.length - o1.length)
}
 
function matchWords(data, keywords) {
  return doMatch(data, keywords).map(e => e.matched)
}
 
function matchKeywords(data, keywords) {
  return doMatch(data, keywords).map(e => e.keyword)
}
 
function pickWords(mats, count = 1) {
  let result = ''
  const items = count === 1 ? [mats[0]] : mats
  items.forEach(mat => {
      if (!mat || mat.length == 0) {
          return
      }
      const words = mat[1] || mat[0]
      result += (result.length > 0) ? ',' + words : words
  })
  return result
}
 
export default {
  format,
  doMatch,
  matchWords,
  matchKeywords,
  pickWords,
}
